import LXWindowUtil
import uiautomation as uia

import logging
wxlog = logging.getLogger('wxauto')
wxlog.setLevel(logging.DEBUG)



# 唤醒微信窗口（确保微信已登录）
LXWindowUtil.OpenWindow(classname="WeChatMainWndForPC")

# 获取打开微信窗口句柄
wx_window = uia.WindowControl(Name="微信", ClassName="WeChatMainWndForPC")

def searchChat(name:str):
    # 跳转到指定群聊窗口
    wx_window.SendKeys('{Ctrl}f', waitTime=0.5)  # 打开搜索框
    wx_window.SendKeys('{Ctrl}a', waitTime=0.2)  # 清空搜索框
    wx_window.SendKey(uia.SpecialKeyNames['DELETE'], waitTime=0.2)
    wx_window.EditControl(Name="搜索").SendKeys(name, waitTime=0.5)
    wx_window.SendKey(uia.SpecialKeyNames['ENTER'], waitTime=0.5)  # 进入群聊

searchChat("群聊天测试")


# def selectChat(who:str, savepic=False, savefile=False, savevoice=False):
#     exists = uia.WindowControl(searchDepth=1, ClassName='ChatWnd', Name=who).Exists(maxSearchSeconds=0.1)
#     if not exists:
#         ChatWith(who)
#         SessionBox.ListItemControl(RegexName=who).DoubleClick(simulateMove=False)


def sendMsg(msg:str):
    # 发送消息
    wx_window.SendKeys(msg, waitTime=1)
    uia.SendKeys('{ENTER}')  # 发送消息

# sendMsg("你好")
